
package tools;



import configs.ServerConfig;

import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


















 public class MapleAESOFB
         {
    /*  22 */   private static final byte[] funnyBytes = new byte[]{-20, 63, 119, -92, 69, -48, 113, -65, -73, -104, 32, -4, 75, -23, -77, -31, 92, 34, -9, 12, 68, 27, -127, -67, 99, -115, -44, -61, -14, 16, 25, -32, -5, -95, 110, 102, -22, -82, -42, -50, 6, 24, 78, -21, 120, -107, -37, -70, -74, 66, 122, 42, -125, 11, 84, 103, 109, -24, 101, -25, 47, 7, -13, -86, 39, 123, -123, -80, 38, -3, -117, -87, -6, -66, -88, -41, -53, -52, -110, -38, -7, -109, 96, 45, -35, -46, -94, -101, 57, 95, -126, 33, 76, 105, -8, 49, -121, -18, -114, -83, -116, 106, -68, -75, 107, 89, 19, -15, 4, 0, -10, 90, 53, 121, 72, -113, 21, -51, -105, 87, 18, 62, 55, -1, -99, 79, 81, -11, -93, 112, -69, 20, 117, -62, -72, 114, -64, -19, 125, 104, -55, 46, 13, 98, 70, 23, 17, 77, 108, -60, 126, 83, -63, 37, -57, -102, 28, -120, 88, 44, -119, -36, 2, 100, 64, 1, 93, 56, -91, -30, -81, 85, -43, -17, 26, 124, -89, 91, -90, 111, -122, -97, 115, -26, 10, -34, 43, -103, 74, 71, -100, -33, 9, 118, -98, 48, 14, -28, -78, -108, -96, 59, 52, 29, 40, 15, 54, -29, 35, -76, 3, -40, -112, -56, 60, -2, 94, 50, 36, 80, 31, 58, 67, -118, -106, 65, 116, -84, 82, 51, -16, -39, 41, Byte.MIN_VALUE, -79, 22, -45, -85, -111, -71, -124, Byte.MAX_VALUE, 97, 30, -49, -59, -47, 86, 61, -54, -12, 5, -58, -27, 8, 73};







       private final short mapleVersion;







       private byte[] iv;






       private Cipher cipher;









    public MapleAESOFB(byte[] iv, short mapleVersion) {

        try {
            /*  55 */
            this.cipher = Cipher.getInstance("AES");
            /*  56 */
            this.cipher.init(1, SecretKeySpecFactory.Companion.getSecretKeySpace(ServerConfig.LOGIN_MAPLE_VERSION % 20));
            /*  57 */
        } catch (NoSuchAlgorithmException | javax.crypto.NoSuchPaddingException e) {
            /*  58 */
            System.err.print("错误 " + e);
            /*  59 */
        } catch (InvalidKeyException e) {
            /*  60 */
            System.err.print("启动加密计算工具错误.请确实你是否使用了无限制加固型密码系统的(.jar)文件." + e);

        }
        /*  62 */
        setIv(iv);
        /*  63 */
        this.mapleVersion = (short) (mapleVersion >> 8 & 0xFF | mapleVersion << 8 & 0xFF00);

    }



    public MapleAESOFB(byte[] key, byte[] iv, short mapleVersion) {
        /*  67 */
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

        try {
            /*  69 */
            this.cipher = Cipher.getInstance("AES");
            /*  70 */
        } catch (NoSuchAlgorithmException | javax.crypto.NoSuchPaddingException e) {
            /*  71 */
            System.err.print("错误 " + e);

        }

        try {
            /*  74 */
            this.cipher.init(1, skeySpec);
            /*  75 */
        } catch (InvalidKeyException e) {
            /*  76 */
            System.err.print("启动加密计算工具错误.请确实你是否使用了无限制加固型密码系统的(.jar)文件." + e);

        }
        /*  78 */
        setIv(iv);
        /*  79 */
        this.mapleVersion = (short) (mapleVersion >> 8 & 0xFF | mapleVersion << 8 & 0xFF00);

    }









    public static int getPacketLength(int packetHeader) {
        /*  89 */
        int packetLength = packetHeader >>> 16 ^ packetHeader & 0xFFFF;
        /*  90 */
        packetLength = packetLength << 8 & 0xFF00 | packetLength >>> 8 & 0xFF;
        /*  91 */
        return packetLength;

    }









    public static int getPacketLength(byte[] packetHeader) {
        /* 101 */
        if (packetHeader.length < 4) {
            /* 102 */
            return -1;

        }
        /* 104 */
        return (packetHeader[0] ^ packetHeader[2]) & 0xFF | (packetHeader[1] ^ packetHeader[3]) << 8 & 0xFF00;

    }










    public static byte[] getNewIv(byte[] oldIv) {
        /* 115 */
        byte[] in = {-14, 83, 80, -58};
        /* 116 */
        for (int x = 0; x < 4; x++) {
            /* 117 */
            funnyShit(oldIv[x], in);

        }

        /* 120 */
        return in;

    }













    public static void funnyShit(byte inputByte, byte[] in) {
        /* 134 */
        byte elina = in[1];
        /* 135 */
        byte moritz = funnyBytes[elina & 0xFF];
        /* 136 */
        moritz = (byte) (moritz - inputByte);
        /* 137 */
        in[0] = (byte) (in[0] + moritz);
        /* 138 */
        moritz = in[2];
        /* 139 */
        moritz = (byte) (moritz ^ funnyBytes[inputByte & 0xFF]);
        /* 140 */
        elina = (byte) (elina - (moritz & 0xFF));
        /* 141 */
        in[1] = elina;
        /* 142 */
        elina = in[3];
        /* 143 */
        moritz = elina;
        /* 144 */
        elina = (byte) (elina - (in[0] & 0xFF));
        /* 145 */
        moritz = funnyBytes[moritz & 0xFF];
        /* 146 */
        moritz = (byte) (moritz + inputByte);
        /* 147 */
        moritz = (byte) (moritz ^ in[2]);
        /* 148 */
        in[2] = moritz;
        /* 149 */
        elina = (byte) (elina + (funnyBytes[inputByte & 0xFF] & 0xFF));
        /* 150 */
        in[3] = elina;

        /* 152 */
        int merry = in[0] & 0xFF;
        /* 153 */
        merry |= in[1] << 8 & 0xFF00;
        /* 154 */
        merry |= in[2] << 16 & 0xFF0000;
        /* 155 */
        merry |= in[3] << 24 & 0xFF000000;
        /* 156 */
        int ret_value = merry >>> 29;
        /* 157 */
        merry <<= 3;
        /* 158 */
        ret_value |= merry;

        /* 160 */
        in[0] = (byte) (ret_value & 0xFF);
        /* 161 */
        in[1] = (byte) (ret_value >> 8 & 0xFF);
        /* 162 */
        in[2] = (byte) (ret_value >> 16 & 0xFF);
        /* 163 */
        in[3] = (byte) (ret_value >> 24 & 0xFF);

    }









    public static void Morph(byte pValue, byte[] pIv) {
        /* 173 */
        byte tableInput = funnyBytes[pValue];
        /* 174 */
        pIv[0] = (byte) (pIv[0] + (byte) (funnyBytes[pIv[1]] - pValue));
        /* 175 */
        pIv[1] = (byte) (pIv[1] - (byte) (pIv[2] ^ tableInput));
        /* 176 */
        pIv[2] = (byte) (pIv[2] ^ (byte) (funnyBytes[pIv[3]] + pValue));
        /* 177 */
        pIv[3] = (byte) (pIv[3] - (byte) (pIv[0] - tableInput));

        /* 179 */
        int Value = pIv[0] & 0xFF | pIv[1] << 8 & 0xFF00 | pIv[2] << 16 & 0xFF0000 | pIv[3] << 24 & 0xFF000000;
        /* 180 */
        Value = Value >> 29 | Value << 3;

        /* 182 */
        pIv[0] = (byte) (Value & 0xFF);
        /* 183 */
        pIv[1] = (byte) (Value >> 8 & 0xFF);
        /* 184 */
        pIv[2] = (byte) (Value >> 16 & 0xFF);
        /* 185 */
        pIv[3] = (byte) (Value >> 24 & 0xFF);

    }








    public byte[] getIv() {
        /* 194 */
        return this.iv;

    }








    private void setIv(byte[] iv) {
        /* 203 */
        this.iv = iv;

    }










    public byte[] crypt(byte[] data) {
        /* 214 */
        int remaining = data.length;
        /* 215 */
        int llength = 1456;
        /* 216 */
        int start = 0;


        try {
            /* 219 */
            while (remaining > 0) {
                /* 220 */
                byte[] myIv = BitTools.multiplyBytes(this.iv, 4, 4);
                /* 221 */
                if (remaining < llength) {
                    /* 222 */
                    llength = remaining;

                }
                /* 224 */
                for (int x = start; x < start + llength; x++) {
                    /* 225 */
                    if ((x - start) % myIv.length == 0) {
                        /* 226 */
                        byte[] newIv = this.cipher.doFinal(myIv);
                        /* 227 */
                        System.arraycopy(newIv, 0, myIv, 0, myIv.length);

                    }

                    /* 230 */
                    data[x] = (byte) (data[x] ^ myIv[(x - start) % myIv.length]);

                }
                /* 232 */
                start += llength;
                /* 233 */
                remaining -= llength;
                /* 234 */
                llength = 1460;

            }
            /* 236 */
            if (data[0] == 105 && data[1] == 0) {
                /* 237 */
                if (!ServerConfig.CLIENT_LOGIN)
                    /* 238 */ updateIv();

            } else {
                /* 240 */
                updateIv();

            }

            /* 243 */
        } catch (IllegalBlockSizeException | javax.crypto.BadPaddingException e) {
            /* 244 */
            e.printStackTrace();

        }
        /* 246 */
        return data;

    }





    public byte[] 新加密(byte[] 数据) {
        /* 252 */
        int remaining = 数据.length;
        /* 253 */
        int llength = 1456;
        /* 254 */
        int start = 0;
        /* 255 */
        while (remaining > 0) {
            /* 256 */
            if (remaining < llength) {
                /* 257 */
                llength = remaining;

            }
            /* 259 */
            for (int x = start; x < start + llength; x++) {
                /* 260 */
                数据[x] = (byte) (数据[x] + this.iv[0]);

            }
            /* 262 */
            start += llength;
            /* 263 */
            remaining -= llength;
            /* 264 */
            llength = 1460;

        }
        /* 266 */
        updateIv();
        /* 267 */
        return 数据;

    }







    public void updateIv() {
        /* 275 */
        this.iv = getNewIv(this.iv);

    }











    public byte[] getPacketHeader(int length) {
        /* 287 */
        int iiv = (this.iv[3] & 0xFF | this.iv[2] << 8 & 0xFF00) ^ this.mapleVersion;
        /* 288 */
        int mlength = (length << 8 & 0xFF00 | length >>> 8) ^ iiv;

        /* 290 */
        return new byte[]{(byte) (iiv >>> 8 & 0xFF), (byte) (iiv & 0xFF), (byte) (mlength >>> 8 & 0xFF), (byte) (mlength & 0xFF)};

    }







    public byte[] getPacketHeaderN(int length) {
        /* 298 */
        int iiv = (this.iv[3] & 0xFF | this.iv[2] << 8 & 0xFF00) ^ this.mapleVersion;
        /* 299 */
        int mlength = 0;
        /* 300 */
        if (length >= 65280) {
            /* 301 */
            mlength = 0xFF ^ iiv;

        } else {
            /* 303 */
            mlength = (length << 8 & 0xFF00 | length >>> 8) ^ iiv;

        }

        /* 306 */
        if (length >= 65280) {
            /* 307 */
            int packetHeader2 = length ^ (mlength >>> 8 & 0xFF | (mlength & 0xFF) << 8) ^ 0xFF00;
            /* 308 */
            return new byte[]{(byte) (iiv >>> 8 & 0xFF), (byte) (iiv & 0xFF), (byte) (mlength >>> 8 & 0xFF), (byte) (mlength & 0xFF), (byte) (packetHeader2 & 0xFF), (byte) (packetHeader2 >>> 8 & 0xFF), (byte) (packetHeader2 >>> 16 & 0xFF), (byte) (packetHeader2 >>> 24 & 0xFF)};

        }
        /* 310 */
        return new byte[]{(byte) (iiv >>> 8 & 0xFF), (byte) (iiv & 0xFF), (byte) (mlength >>> 8 & 0xFF), (byte) (mlength & 0xFF)};

    }




















    public boolean checkPacket(byte[] packet) {
        /* 331 */
        return (((packet[0] ^ this.iv[2]) & 0xFF) == (this.mapleVersion >> 8 & 0xFF) && ((packet[1] ^ this.iv[3]) & 0xFF) == (this.mapleVersion & 0xFF));

    }











    public boolean checkPacket(int packetHeader) {
        /* 343 */
        return checkPacket(new byte[]{(byte) (packetHeader >> 24 & 0xFF), (byte) (packetHeader >> 16 & 0xFF)});

    }









    public String toString() {
        /* 353 */
        return "IV: " + HexTool.toString(this.iv);

    }

}


/* Location:              C:\PGX\Server.jar!\tools\MapleAESOFB.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */